package com.woniuxy.purchasemanager.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.commons.entity.purchaseManager.*;
import com.woniuxy.commons.entity.purchaseManager.base.VoEntity;
import com.woniuxy.commons.entity.purchaseManager.dto.NeedGoodsDto;
import com.woniuxy.commons.utils.PageUtils;
import com.woniuxy.commons.utils.Query;
import com.woniuxy.purchasemanager.dao.*;
import com.woniuxy.purchasemanager.service.NeedService;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;


@Service("needService")
public class NeedServiceImpl extends ServiceImpl<NeedDao, NeedEntity> implements NeedService {

    @Resource
    private NeedAndGoodsDao needAndGoodsDao;

    @Resource
    private NeedAllocationDao needAllGoodsDao;

    @Resource
    private DemandGoodsDao demandGoodsDao;

    @Resource
    private NeedDao needDao;

    @Resource
    private NeedAllocationAndNeedDemandGoods needallocation;

    @Resource
    private NeedDemandGoodsDao needDemandGoodsDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NeedEntity> page = this.page(
                new Query<NeedEntity>().getPage(params),
                new QueryWrapper<NeedEntity>()
        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public boolean saveGoodsAll(HashMap<String, Object> map)  {
        Double totalWeight = 0.0; //存入总的重量
        Integer totalAccount = 0; //存入需求单总的数量
        HashMap<String, Object> basicInfo = (HashMap<String, Object>) map.get("basicInfo");
        ArrayList<HashMap<String, Object>> goodsInfo = (ArrayList<HashMap<String, Object>>) map.get("goodsInfo");
        NeedEntity needEntity = JSON.parseObject(JSON.toJSONString(basicInfo), NeedEntity.class);

        List<Double> arrayweight = new ArrayList<>();  //重量集合
        List<Integer> arrlistAccount = new ArrayList<>(); //数量集合
        List<Integer> arrlistId = new ArrayList<>(); //返回ID集合
        for (HashMap<String, Object> stringObjectHashMap : goodsInfo) {
            DemandGoodsEntity demandGoodsEntity = JSON.parseObject(JSON.toJSONString(stringObjectHashMap), DemandGoodsEntity.class);
            Double gramStart = demandGoodsEntity.getGramStart();
            Double gramEnd = demandGoodsEntity.getGramEnd();
            Double weight = demandGoodsEntity.getWeight();
            demandGoodsEntity.setGramWeight(gramStart + "~" + gramEnd); //克重范围
            Integer amount = demandGoodsEntity.getAmount();
            arrlistAccount.add(amount); //获取数量
            arrayweight.add(weight);
            demandGoodsDao.insert(demandGoodsEntity);
            arrlistId.add(demandGoodsEntity.getGoodsId());
        }

        for (int i = 0; i < arrayweight.size(); i++) {
            totalWeight += arrayweight.get(i);
        }

        for (int i = 0; i < arrlistAccount.size(); i++) {
            totalAccount += arrlistAccount.get(i);
        }
        needEntity.setAmount(totalAccount);
        needEntity.setWeight(totalWeight);
        needEntity.setGoodsAmount(totalAccount);
        needEntity.setGoodsWeight(totalWeight);
        needEntity.setCreatedBy(needEntity.getClient());
        baseMapper.insert(needEntity);
        Integer needId = needEntity.getNeedId();  //插入 need 表返回的ID
        for (int i = 0; i < arrlistId.size(); i++) {
            needAndGoodsDao.insert(needId, arrlistId.get(i));
        }

        //todo 插入分配需求单表
        List<Double> arrayweight1 = new ArrayList<>();  //重量集合
        List<Integer> arrlistAccount1 = new ArrayList<>(); //数量集合
        List<Integer> arrlistId1 = new ArrayList<>(); //返回ID集合
        NeedAllocationEntity needAllocationEntity = JSON.parseObject(JSON.toJSONString(needEntity), NeedAllocationEntity.class);
        System.out.println(needAllocationEntity);
        //todo 插入分配需求单商品表
        NeedDemandGoodsEntity needDemandGoodsEntity = new NeedDemandGoodsEntity();
        for (HashMap<String, Object> stringObjectHashMap : goodsInfo) {
            DemandGoodsEntity demandGoodsEntity = JSON.parseObject(JSON.toJSONString(stringObjectHashMap), DemandGoodsEntity.class);
            needDemandGoodsEntity.setGramStart(demandGoodsEntity.getGramStart());
            needDemandGoodsEntity.setGramEnd(demandGoodsEntity.getGramEnd());
            needDemandGoodsEntity.setNeedDemandProductId(demandGoodsEntity.getGoodsCode());
            needDemandGoodsEntity.setGramWeight(demandGoodsEntity.getGramStart() + "~" + demandGoodsEntity.getGramEnd()); //克重范围
            System.out.println(demandGoodsEntity);
            Integer amount = demandGoodsEntity.getAmount(); //数量
            arrlistAccount1.add(amount); //获取数量
            needDemandGoodsDao.insert(needDemandGoodsEntity);
            arrlistId1.add(needDemandGoodsEntity.getAllocationDemandId());
        }

        needAllGoodsDao.insert(needAllocationEntity);
        Integer allocationDemandId = needAllocationEntity.getAllocationDemandId();
        //插入中间表
        for (int i = 0; i < arrlistId1.size(); i++) {
            needallocation.insert(allocationDemandId, arrlistId1.get(i));
        }
        return true;
    }

    @Override
    public VoEntity<NeedEntity> pageList(Integer currentPage, Integer pageSize) {
        VoEntity voEntity = new VoEntity<>();
        IPage<NeedEntity> page = new Page<>(currentPage, pageSize);//参数一是当前页，参数二是每页个数
        baseMapper.selectPage(page, null);
        voEntity.setSize(pageSize);
        voEntity.setTotal(page.getTotal());
        voEntity.setCurrent(currentPage);
        voEntity.setList(page.getRecords());
        return voEntity;
    }

    @Override
    public List<NeedEntity> selectAll() {
        return needDao.selectList(new QueryWrapper<>());
    }


}